home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Eudora Stat Server / rvf-hack / reportrev3.pl < prev   
Encoding:
Perl Script  |  2001-06-23  |  2.4 KB  |  139 lines

  1. #!/usr/bin/perl
  2.  
  3. #
  4. # use CGI.pm and DBI external modules
  5. #
  6. # CGI.pm is part of standard perl distribution
  7. # DBI is available from www.cpan.org
  8. #
  9. # program uses CSV files to store data
  10. #
  11. # work in progress, inserts graphs generated with
  12. # GD::Graph library.  server was down during MacHack
  13. # so we couldn't get it complied on Mac OS X
  14. #
  15.  
  16. use CGI;
  17. use DBI;
  18. use CGI::Carp qw(fatalsToBrowser);
  19.  
  20. my $q = new CGI;
  21.  
  22. sub print_table {
  23.  
  24. my $description = shift;
  25. my $variable = shift;
  26. my $graph = shift;
  27.  
  28. #
  29. # set up for database access
  30. # ref: O'Reilley CGI Programming with Perl
  31. #
  32.  
  33. my $dbdir = "/Library/WebServer/CGI-Executables";
  34. #my $dbdir = "C:/Documents and Settings/richard/My Documents/Projects/machack";
  35. my $dbh = DBI->connect("DBI:CSV:f_dir=$dbdir")
  36.         or die "Can't connect: " . $DBI::errstr;
  37.  
  38. my $sql = "select user, $variable from data order by $variable";
  39.  
  40. my $sth = $dbh->prepare($sql)
  41.         or die "Can't prepare: " . $dbh->errstr();
  42.  
  43. $sth->execute()
  44.         or die "Can not execute: " . $sth->errstr();
  45.  
  46. my @row;
  47.  
  48. print qq`
  49. <table border="0">
  50. <tr>
  51. <td>
  52. <table border="1">
  53. <tr><td>User</td><td>$description</td></tr>
  54. `;
  55.  
  56. my $i = 0;
  57. my @list;
  58. my $msg_avg;
  59.  
  60. while (@row = $sth->fetchrow_array()){
  61.     @list[$i] = join( "</td><td>", @row) . "</td></tr>\n";
  62.     $i++;
  63. }
  64.  
  65. #while (@row = $sth->fetchrow_array()){
  66. ##        print ("<tr><td>");
  67. #        $msg_avg = int $row[1] / $row[2];
  68. #        if ($variable eq "usage_avg_day"){
  69. #                $msg_avg = int $msg_avg / 60;
  70. #    }
  71. #        @list[$i] = "<tr><td>" .$row[0] . "</td><td>" . $msg_avg . "</td></tr>\n
  72. #";
  73. ##        @list[$i] = join( "</td><td>" , @row) . "</td></tr>\n";
  74. #        print @list[$i];
  75. #        $i++;
  76. #}
  77.  
  78. while ($i >= 0){
  79.     print ("<tr><td>");
  80.     print @list[$i];
  81.     $i--;
  82. }
  83.  
  84. print qq`
  85. </td>
  86. </table>
  87. <td><img src="http://10.1.1.114/$graph"></td>
  88. </tr>
  89. </table>
  90. <br>
  91. `;
  92.  
  93. $sth->finish;
  94.  
  95. $dbh->disconnect;
  96. }
  97.  
  98. sub print2{
  99.  
  100. print ("<html><body><h1>blah</h1></body></html>\n");
  101.  
  102. }
  103.  
  104. #
  105. # tell browser what mime type file is
  106. #
  107.  
  108. print $q->header("text/html");
  109.  
  110. print qq`
  111. <html>
  112. <head>
  113. </head>
  114. <body bgcolor="FFCC33">
  115. `;
  116.  
  117. print qq`
  118. <h2>Sorted by read messages</h2>
  119. `;
  120.  
  121. &print_table("Read Messages", "read_msg_avg_day", "graph1.png");
  122.  
  123. print qq`
  124. <h2>Sorted by sent messages</h2>
  125. `;
  126.  
  127. &print_table("Sent Messages", "sent_msg_avg_day", "graph2.png");
  128.  
  129. print qq`
  130. <h2>Sorted by usage</h2>
  131. `;
  132.  
  133. &print_table("Usage", "usage_avg_day", "graph3.png");
  134.  
  135. print qq`
  136. </body>
  137. </html>
  138. `;
  139.